package com.asohofrucol.web;

import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;

import com.asohofrucol.dto.AsohofrucolSessionDTO;
import com.asohofrucol.dto.ForestResourceRegisterDTO;
import com.asohofrucol.service.UserOwnerService;

public class ForestBusinessController {
	@Autowired
	private AsohofrucolSessionDTO aSession;
	@Autowired
	private UserOwnerService userOwnerService;	

	public void createNewRegister(ActionEvent actionEvent) {
		if (aSession.getForestResource().getForestResourceRegisters().size() < 3) {
			Long ruatId = aSession.getSelectedRuatRegister().getId();
			ForestResourceRegisterDTO forestResourceRegisterDTO = new ForestResourceRegisterDTO();
			forestResourceRegisterDTO.setRuatId(ruatId);
			forestResourceRegisterDTO.setStatus("1");
			aSession.getForestResource().getForestResourceRegisters()
					.add(forestResourceRegisterDTO);
		}
	}
	public void deleteRegister(ActionEvent actionEvent) {
		ForestResourceRegisterDTO selected = (ForestResourceRegisterDTO) actionEvent
				.getComponent().getAttributes().get("register");
		aSession.getForestResource().getForestResourceRegisters()
				.remove(selected);
		if(selected.getId()!=null){
			String userModifier = aSession.getUserInfo().getUserName(); 
			userOwnerService.deleteForestResourceRegister(selected, userModifier);
		}
	}
}
